package com.education.service.infrastructure.apigateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 统一的的跨域配置
 * Created by Lance on 2020/07/07 22:54
 */
@Configuration
public class CorsConfig {

    /**
     * 跨域的过滤器
     * @return
     */
    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        // 开启跨域策略
        config.setAllowCredentials(true);
        // 跨域
        config.addAllowedOrigin("*");
        // 是否接收所有的请求头
        config.addAllowedHeader("*");
        // 是否接收所有的方法
        config.addAllowedMethod("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);

        return new CorsWebFilter(source);
    }
}
